package net.zjitc.controller;

import net.zjitc.pojp.CateGory;
import net.zjitc.service.AdminCateGoryService;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.AdminCateGoryServiceImpl;
import net.zjitc.service.impl.UserServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by Administrator on 2017/11/15.
 */
public class AdminCateGory {
    AdminCateGoryService adminCateGoryService=new AdminCateGoryServiceImpl();
    //显示列表
    @URLMapping(url="/AdminCategoryFindAll")
    public String findAll(HttpServletRequest request, HttpServletResponse response){

       List<CateGory> list= adminCateGoryService.findOneList();
        request.setAttribute("list",list);
        return "/admin/category/onelist.jsp";
    }
    @URLMapping(url = "/AdminCategoryAddUI")
    public String addUI(HttpServletRequest request, HttpServletResponse response){


        return "/admin/category/add.jsp";
    }

    //添加页面
    @URLMapping(url = "/AdminCategoryAdd")
    public String add(HttpServletRequest request, HttpServletResponse response){
        CateGory cateGory=new CateGory();
        try {
            BeanUtils.populate(cateGory,request.getParameterMap());
            int count=adminCateGoryService.insert(cateGory);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //添加完后。要重定向到...
        String url=request.getContextPath()+"/AdminCategoryFindAll";
        return "Redirect:"+url;

    }
    //显示修改页面
    @URLMapping(url = "/AdminCategoryEditUI")
    public String editUI(HttpServletRequest request, HttpServletResponse response){
      String cid=request.getParameter("cid");
        CateGory cateGory=adminCateGoryService.findByCid(cid);
        request.setAttribute("cc",cateGory);
        return "/admin/category/editUI.jsp";
    }

    //更新功能
    @URLMapping(url = "/AdminCategoryEdit")
    public String edit(HttpServletRequest request, HttpServletResponse response){
       CateGory cateGory=new CateGory();
        try {
            BeanUtils.populate(cateGory,request.getParameterMap());
            int count=adminCateGoryService.update(cateGory);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Redirect:"+request.getContextPath()+"/AdminCategoryFindAll";
    }
    //删除功能
    @URLMapping(url = "/AdminCategoryDelete")
    public String delete(HttpServletRequest request,HttpServletResponse response){
       return "Redirect:"+request.getContextPath()+"/AdminCategoryFindAll";
    }
}
